package com.chenziruo.securitydemo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenziruo.securitydemo.config.DBUserDetailsManager;
import com.chenziruo.securitydemo.entity.User;
import com.chenziruo.securitydemo.mapper.UserMapper;
import com.chenziruo.securitydemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private DBUserDetailsManager dbUserDetailsManager;

    @Override
    public void saveUserDetails(User user) {
        Collection<GrantedAuthority> authorities = Arrays.asList(new SimpleGrantedAuthority("ADMINISTRATOR"));
        System.out.println(user);
        UserDetails userDetails = org.springframework.security.core.userdetails.User
                .withDefaultPasswordEncoder()//使用默认的密码加密方案
                .username(user.getUsername())
                .password(user.getPassword())
                .authorities(authorities)
                .build();
        dbUserDetailsManager.createUser(userDetails);
    }
}